/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.miami.unix_storage.xmlinventory.classes;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.logging.Logger;

/**
 *
 * @author alorenzo
 */
public class XMLInventorySimpleReader {
    private BufferedReader reader = null;
    private String towrite = null;
    private Path path = null;
    private Charset charset = Charset.forName("UTF-8");


    public String getToWrite() {
        return towrite;
    }

    public void setToWrite(String towrite) {
        this.towrite = towrite;
    }
    XMLInventorySimpleReader(String setpath, String filetoreadfrom){
        this.path = FileSystems.getDefault().getPath(setpath, filetoreadfrom);
    }
    public String getPathFile(){
        
        return this.path.toString();
    }
    public String WriteToFile(){
        if (getPathFile()==null){
            return "No Path Set!";
        } else {
            try (BufferedWriter writer = Files.newBufferedWriter(this.path, 
                    this.charset, StandardOpenOption.APPEND)){
            if (getToWrite() == null){
                    return "No Data to write, please call setToWrite method.";
            } else {
                    writer.write(getToWrite(), 0, getToWrite().length());
                    writer.close();
                    return String.format("String has been written to file:"
                            + " %s", getPathFile());
                    }
            } catch (IOException ex) {
                System.err.format ("IOException: %s%n", ex);
                return String.format("IOException: %s%n", ex);
            }
        }
    }
}
